package xyz.archknight.app.loginput.dictionary.gen.ui

import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import kotlinx.coroutines.swing.Swing
import org.jetbrains.exposed.sql.transactions.transaction
import xyz.archknight.app.loginput.dictionary.gen.bean.DatabaseProtocol
import xyz.archknight.app.loginput.dictionary.gen.bean.RecordItem
import xyz.archknight.app.loginput.dictionary.gen.dao.Word
import xyz.archknight.app.loginput.dictionary.gen.service.EventBus
import xyz.archknight.app.loginput.dictionary.gen.ui.component.ProgressDialog
import java.io.File
import java.io.FileReader
import java.io.IOException
import java.io.LineNumberReader
import java.util.regex.Pattern
import javax.swing.JFrame
import javax.swing.JOptionPane

/**
 * 从.txt文件导入词库内容的对话框
 *
 * @author midnite
 * create at 2020-06-18 5:57 下午
 */
class ImportDialog(owner: JFrame) : ProgressDialog(owner, "正在导入词库") {
    private var lines: Int = 0

    fun import(filePath: String) {
        GlobalScope.launch(Dispatchers.IO) {
            try {
                val sourceFile = File(filePath)
                LineNumberReader(FileReader(sourceFile)).use { reader ->
                    reader.mark(0)
                    reader.skip(sourceFile.length())
                    lines = reader.lineNumber
                    launch(Dispatchers.Swing) {
                        progressBar.minimum = 0
                        progressBar.value = 0
                        progressBar.maximum = lines
                        progressLabel.text = "${progressBar.value} / ${progressBar.maximum}"
                    }
                }
                LineNumberReader(FileReader(sourceFile)).useLines { results ->
                    transaction {
                        results
                            .map { str -> str.split(Pattern.compile("\\s+")) }
                            .map { list -> RecordItem(list[0].trim(), list[1].trim()) }
                            .forEach { record ->
                                launch(Dispatchers.Swing) {
                                    processingItem.text = "处理 ${record.phase}"
                                    progressBar.value++
                                    progressLabel.text = "${progressBar.value} / ${progressBar.maximum}"
                                }
                                Word.new {
                                    phase = record.phase
                                    pinyin = record.pinyin
                                }
                            }
                    }
                }
            } catch (e: IOException) {
                JOptionPane.showMessageDialog(
                    this@ImportDialog,
                    "指定文件导入失败！",
                    "导入失败",
                    JOptionPane.ERROR_MESSAGE
                )
            }
            launch(Dispatchers.Swing) {
                isVisible = false
                EventBus.dbChange(DatabaseProtocol.RefreshMain)
            }
        }
    }
}